package zust.se.delicious.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import zust.se.delicious.dao.BookmarkDao;
import zust.se.delicious.domain.Bookmark;
import zust.se.delicious.domain.Tags;
import zust.se.delicious.domain.User;

public class BookmarkDaoImpl extends HibernateDaoSupport implements BookmarkDao {

	@Override
	public Bookmark get(Integer id) {
		return getHibernateTemplate().get(Bookmark.class, id);
	}

	@Override
	public Integer save(Bookmark Bookmark) {
		return (Integer) getHibernateTemplate().save(Bookmark);
	}

	@Override
	public void update(Bookmark Bookmark) {
		getHibernateTemplate().update(Bookmark);

	}

	@Override
	public void delete(Bookmark Bookmark) {
		getHibernateTemplate().delete(Bookmark);

	}

	@Override
	public void delete(Integer id) {
		getHibernateTemplate().delete(get(id));

	}

	@Override
	public List<Bookmark> findAll() {
		return (List<Bookmark>) getHibernateTemplate().find("from Bookmark");
	}

	@Override
	public Bookmark findBookmarkByUrl(String url) {
		List<Bookmark> pays = (List<Bookmark>) getHibernateTemplate().find("from Bookmark as b where b.url like '%" + url + "%'  order by createTime desc",
				new Object[] { url });
		if (pays != null && pays.size() > 0) {
			return pays.get(0);
		}
		return null;
	}

	@Override
	public List<Bookmark> findBookmarkByUid(int uid) {
		List<Bookmark> bookmarklist = (List<Bookmark>) getHibernateTemplate().find("from Bookmark where user_id=?", uid);
		return bookmarklist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Long countBmByUrl(final String url) {
		return getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select count(*) from Bookmark where url=:url";
				return session.createQuery(hql).setString("url", url).iterate().next();

			}

		});
	}

	@Override
	public Long countBmByOwnId(final Integer uid) {
		return getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select count(*) from Bookmark where user=:user";
				return session.createQuery(hql).setEntity("user", new User(uid)).iterate().next();
			}

		});
	}

	public List<Bookmark> findBmList(final User user, final int page, final int size) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "from Bookmark where user=:user  order by createTime desc";
				return session.createQuery(hql).setEntity("user", user).setFirstResult((page - 1) * size).setMaxResults(size).list();
			}

		});
	}

	@Override
	public Long countBmList(final Integer uid) {
		return getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select count(*) from Bookmark where user=:user";
				return session.createQuery(hql).setEntity("user", new User(uid)).iterate().next();
			}

		});
	}

	@Override
	public List<Bookmark> findBmFeed(final User user, final int page, final int size) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "from Bookmark where user in (select following from Follow where follower=:buser)  order by createTime desc";
				return session.createQuery(hql).setEntity("buser", user).setFirstResult((page - 1) * size).setMaxResults(size).list();
			}

		});
	}

	@Override
	public Long countBmFeed(final Integer uid) {
		return getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select count(*) from Bookmark where user in (select following from Follow where follower=:buser)";
				return session.createQuery(hql).setEntity("buser", new User(uid)).iterate().next();
			}

		});
	}

	@Override
	public List<Bookmark> searchMyList(final String keyword, final int uid) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select bm from Bookmark bm inner join bm.tagsSet tag where tag.tags_name=:keyword and bm.user=:user order by bm.createTime desc" ;
				return session.createQuery(hql).setString("keyword", keyword).setEntity("user", new User(uid)).setMaxResults(5).list();
			}

		});
	}

	@Override
	public List<Bookmark> searchOtherList(final String keyword, final int uid) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select bm from Bookmark bm inner join bm.tagsSet tag where tag.tags_name=:keyword and bm.user!=:user  order by bm.createTime desc";
				return session.createQuery(hql).setString("keyword", keyword).setEntity("user", new User(uid)).setMaxResults(10).list();
			}
		});
	}

	public List<Bookmark> searchAllList(final String keyword) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "select bm from Bookmark bm inner join bm.tagsSet tag where tag.tags_name=:keyword  order by bm.createTime desc";
				Query query = session.createQuery(hql).setString("keyword", keyword).setMaxResults(10);
				return query.list();
			}

		});
	}

	@Override
	public List<Bookmark> matchMyList(final String url, final int uid) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "from Bookmark where url=:url and user=:user  order by createTime desc";
				return session.createQuery(hql).setString("url", url).setEntity("user", new User(uid)).setMaxResults(10).list();
			}

		});
	}

	@Override
	public List<Bookmark> matchchOtherList(final String url, final int uid) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "from Bookmark where url=:url and user!=:user order by createTime desc";
				return session.createQuery(hql).setString("url", url).setEntity("user", new User(uid)).setMaxResults(10).list();
			}

		});
	}

	@Override
	public List<Bookmark> mathchAllList(final String url) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "from Bookmark where url=:url order by createTime desc";
				return session.createQuery(hql).setString("url", url).setMaxResults(10).list();
			}

		});
	}

}
